package dobest.ray.com.jiongjiong.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dobest.ray.corelibs.AppManager;
import com.dobest.ray.corelibs.utils.FragmentController;
import com.dobest.ray.corelibs.utils.ToastMgr;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.HomePage.HomePageFragment;
import dobest.ray.com.jiongjiong.activity.action.GoIntoActionFragment;
import dobest.ray.com.jiongjiong.activity.mine.MineFragment;
import dobest.ray.com.jiongjiong.activity.wanttoknow.WantToKnowFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout ll_vision;
    private ImageView iv_vision;
    private TextView tv_vision;

    private LinearLayout ll_action;
    private ImageView iv_action;
    private TextView tv_action;

    private LinearLayout ll_wantto_know;
    private ImageView iv_wantto_know;
    private TextView tv_wantto_know;

    private LinearLayout ll_home_page_mine;
    private ImageView iv_home_page_mine;
    private TextView tv_home_page_mine;

    /**
     * fragment control center
     */
    private FragmentController mFragmentController;
    public FragmentManager fragmentManager;
    private String[] fragmentTags = {"fragment_home_page", "fragment_action", "fragment_wantto_know",
            "fragment_mine"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        init();
        addListeners();

    }

    @Override
    public void findViews() {
        ll_vision = (LinearLayout) findViewById(R.id.ll_vision);
        iv_vision = (ImageView) findViewById(R.id.iv_vision);
        tv_vision = (TextView) findViewById(R.id.tv_vision);

        ll_action = (LinearLayout) findViewById(R.id.ll_action);
        iv_action = (ImageView) findViewById(R.id.iv_action);
        tv_action = (TextView) findViewById(R.id.tv_action);

        ll_wantto_know = (LinearLayout) findViewById(R.id.ll_wantto_know);
        iv_wantto_know = (ImageView) findViewById(R.id.iv_wantto_know);
        tv_wantto_know = (TextView) findViewById(R.id.tv_wantto_know);

        ll_home_page_mine = (LinearLayout) findViewById(R.id.ll_home_page_mine);
        iv_home_page_mine = (ImageView) findViewById(R.id.iv_home_page_mine);
        tv_home_page_mine = (TextView) findViewById(R.id.tv_home_page_mine);

        fragmentManager = getSupportFragmentManager();

        mFragmentController = new FragmentController(getSupportFragmentManager(), R.id.fl_contain, fragmentTags);
        mFragmentController.add(HomePageFragment.class, fragmentTags[0], null);

    }

    @Override
    public void init() {

    }

    @Override
    public void addListeners() {
        ll_vision.setOnClickListener(this);
        ll_action.setOnClickListener(this);
        ll_wantto_know.setOnClickListener(this);
        ll_home_page_mine.setOnClickListener(this);
    }

    private int currentIndex = 0;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_vision:
                if (currentIndex == 0) {
                    return;
                }
                currentIndex = 0;
                mFragmentController.add(HomePageFragment.class, fragmentTags[0], null);
                iv_action.setImageResource(R.mipmap.img_home_page_action_no);
                iv_wantto_know.setImageResource(R.mipmap.img_wantto_know_no);
                iv_home_page_mine.setImageResource(R.mipmap.img_homepage_mine);

                tv_action.setTextColor(getResources().getColor(R.color.grey));
                tv_wantto_know.setTextColor(getResources().getColor(R.color.grey));
                tv_home_page_mine.setTextColor(getResources().getColor(R.color.grey));
                break;
            case R.id.ll_action:
                if (currentIndex == 1) {
                    return;
                }
                currentIndex = 1;
                mFragmentController.add(GoIntoActionFragment.class, fragmentTags[1], null);
                iv_action.setImageResource(R.mipmap.img_home_page_action);
                iv_wantto_know.setImageResource(R.mipmap.img_wantto_know_no);
                iv_home_page_mine.setImageResource(R.mipmap.img_homepage_mine);

                tv_action.setTextColor(getResources().getColor(R.color.colorPrimary));
                tv_wantto_know.setTextColor(getResources().getColor(R.color.grey));
                tv_home_page_mine.setTextColor(getResources().getColor(R.color.grey));
                break;
            case R.id.ll_wantto_know:
                if (currentIndex == 2) {
                    return;
                }
                currentIndex = 2;
                mFragmentController.add(WantToKnowFragment.class, fragmentTags[2], null);
                iv_action.setImageResource(R.mipmap.img_home_page_action_no);
                iv_wantto_know.setImageResource(R.mipmap.img_wantto_know);
                iv_home_page_mine.setImageResource(R.mipmap.img_homepage_mine);

                tv_action.setTextColor(getResources().getColor(R.color.grey));
                tv_wantto_know.setTextColor(getResources().getColor(R.color.colorPrimary));
                tv_home_page_mine.setTextColor(getResources().getColor(R.color.grey));
                break;
            case R.id.ll_home_page_mine:
//                if (AuthorityContext.getContext().showPersonCenter(this)) {
                    if (currentIndex == 3) {
                        return;
                    }
                    currentIndex = 3;
                    mFragmentController.add(MineFragment.class, fragmentTags[3], null);
                    iv_action.setImageResource(R.mipmap.img_home_page_action_no);
                    iv_wantto_know.setImageResource(R.mipmap.img_wantto_know_no);
                    iv_home_page_mine.setImageResource(R.mipmap.img_homepage_mine_selected);

                    tv_action.setTextColor(getResources().getColor(R.color.grey));
                    tv_wantto_know.setTextColor(getResources().getColor(R.color.grey));
                    tv_home_page_mine.setTextColor(getResources().getColor(R.color.colorPrimary));
//                }
                break;
        }
    }


    private boolean isBackPressed;

    /**
     * 双击退出
     */
    private void doublePressBackToast() {
        if (!isBackPressed) {
            isBackPressed = true;
            ToastMgr.show(getResources().getString(R.string.exit_string));
        } else {
            AppManager.getAppManager().appExit();
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                isBackPressed = false;
            }
        }, 2000);
    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            doublePressBackToast();
            return true;
        } else {
            return super.onKeyUp(keyCode, event);
        }
    }
}
